package com.kaishun.study.excel;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.kaishun.study.entity.KfPatrolItemDetail;
import com.kaishun.study.entity.KfPatrolItemInfo;
import com.kaishun.study.excel.PatrolItemData;
import com.kaishun.study.service.KfPatrolItemDetailService;
import com.kaishun.study.service.KfPatrolItemInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Package: com.kaishun.study.utils
 * @ClassName: ExcelUtils
 * @Author: zhoukaishun
 * @CreateTime: 2021/12/8 18:08
 * @Description: easyExcel
 */
@Component
@Slf4j
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
public class ExcelUtils {

    public String createUser = "sys_add_by_zks";

    public Integer parentId = 0;

    public Integer itemId = 0;

    public Integer itemIdV2 = 0;

    public Integer score = 0;

    public Integer sort = 1;

    public Integer busBusinessId = 11;

    public Map<String, Integer> checkMethodMap = new HashMap() {{
        put("现场核查", 0);
        put("电话核查", 1);
    }};


    @Resource
    private KfPatrolItemInfoService kfPatrolItemInfoService;

    @Resource
    private KfPatrolItemDetailService kfPatrolItemDetailService;

    public static void main(String[] args) {
        String str = "";
        System.out.println(Optional.ofNullable(str).orElse("默认"));
    }

    /**
     * 处理展示区excel
     *
     * @param dataList
     */
    @Transactional(rollbackFor = Exception.class)
    public void handle(List<PatrolItemData> dataList, Integer patrolSceneType) {

        /**
         *   获取自身代理对象  防止事务失效
         */
        ExcelUtils excelUtils = (ExcelUtils)AopContext.currentProxy();
        for (PatrolItemData e : dataList) {
            log.info("读取到一条数据{}", JSON.toJSONString(e));
            if (e.getScore() != null) {
                this.score = Integer.parseInt(e.getScore());
            }
            if (e.getTitle() != null) {
                KfPatrolItemInfo info = initKfPatrolItem(e.getTitle(), 0, 0, patrolSceneType);
                kfPatrolItemInfoService.insert(info);
                this.parentId = info.getId();
                Integer id = excelUtils.insertPatrolItem(info.getId(),e.getName(),patrolSceneType,1);
                this.sort = 1;
                excelUtils.insertPatrolItemDetail(id,e,null);
                continue;
            }
            if (e.getName() != null) {
                Integer id = excelUtils.insertPatrolItem(this.parentId,e.getName(),patrolSceneType,1);
                this.sort = 1;
                excelUtils.insertPatrolItemDetail(id,e,null);
                continue;
            }
            if (e.getTitle() == null && e.getName() == null) {
                excelUtils.insertPatrolItemDetail(this.itemId,e,null);
                continue;
            }
        }

    }

    /**
     * 岗位服务excel单独处理  patrolSceneType 为：0
     *
     * @param dataList
     */
    @Transactional(rollbackFor = Exception.class)
    public void handle(List<PatrolItemData> dataList) {

        /**
         *   获取自身代理对象  防止事务失效
         */
        ExcelUtils excelUtils = (ExcelUtils)AopContext.currentProxy();
        // 岗位服务 patrolSceneType 为：0
        Integer patrolSceneType = 0;
        for (PatrolItemData e : dataList) {
            log.info("读取到一条数据{}", JSON.toJSONString(e));
            if (e.getScore() != null) {
                this.score = Integer.parseInt(e.getScore());
            }

            if (e.getTitle() != null) {
                //title有值，name、item必有值
                KfPatrolItemInfo info = initKfPatrolItem(e.getTitle(), 0, 0, patrolSceneType);
                kfPatrolItemInfoService.insert(info);
                this.parentId = info.getId();
                Integer itemId = excelUtils.insertPatrolItem(info.getId(),e.getName(),patrolSceneType,1);
                Integer id = excelUtils.insertPatrolItemV2(itemId,e.getItem(),patrolSceneType,2);
                this.sort = 1;
                insertPatrolItemDetail(id,e,checkMethodMap.get(e.getCheckMethod()));
                continue;
            }
            if (e.getName() != null) {
                Integer itemId = excelUtils.insertPatrolItem(this.parentId,e.getName(),patrolSceneType,1);
                Integer id = excelUtils.insertPatrolItemV2(itemId,e.getItem(),patrolSceneType,2);
                this.sort = 1;
                insertPatrolItemDetail(this.itemIdV2,e,checkMethodMap.get(e.getCheckMethod()));
                continue;
            }
            if (e.getItem() != null) {
                Integer id = excelUtils.insertPatrolItemV2(this.itemId,e.getItem(),patrolSceneType,2);
                this.sort = 1;
                insertPatrolItemDetail(this.itemIdV2,e,checkMethodMap.get(e.getCheckMethod()));
                continue;
            }
            if (e.getTitle() == null && e.getName() == null && e.getItem() == null) {
                insertPatrolItemDetail(this.itemIdV2,e,checkMethodMap.get(e.getCheckMethod()));
                continue;
            }
        }
    }

    /**
     * 插入 item
     * @param name
     * @param patrolSceneType
     * @param rootPath
     */
    public Integer insertPatrolItem(Integer parentId,String name ,Integer patrolSceneType,Integer rootPath){
        KfPatrolItemInfo info = initKfPatrolItem(name, parentId, rootPath, patrolSceneType);
        kfPatrolItemInfoService.insert(info);
        this.itemId = info.getId();
        return info.getId();
    }

    public Integer insertPatrolItemV2(Integer parentId,String name ,Integer patrolSceneType,Integer rootPath){
        KfPatrolItemInfo info = initKfPatrolItem(name, parentId, rootPath, patrolSceneType);
        kfPatrolItemInfoService.insert(info);
        this.itemIdV2 = info.getId();
        return info.getId();
    }

    /**
     * 插入详情
     * @param data
     * @param checkMethod
     */
    public void insertPatrolItemDetail(Integer itemId,PatrolItemData data,Integer checkMethod){
        KfPatrolItemDetail detail = initKfPatrolItemDetail(itemId, data.getDetail(), this.score,checkMethod);
        kfPatrolItemDetailService.insert(detail);
        this.sort++;
    };

    /**
     * 构建KfPatrolItemDetail
     *
     * @param itemId
     * @param detail
     * @param score
     * @param checkMethod
     * @return
     */
    private KfPatrolItemDetail initKfPatrolItemDetail(Integer itemId, String detail, Integer score,
                                                      Integer checkMethod) {
        KfPatrolItemDetail kfPatrolItemDetail = new KfPatrolItemDetail();
        kfPatrolItemDetail.setChekcMethod(checkMethod);
        kfPatrolItemDetail.setCreateTime(new Date());
        kfPatrolItemDetail.setDeleteFlag(0);
        kfPatrolItemDetail.setCreateUserUuid(this.createUser);
        kfPatrolItemDetail.setItemDescribe(detail);
        kfPatrolItemDetail.setModifyTime(new Date());
        kfPatrolItemDetail.setModifyUserUuid(this.createUser);
        kfPatrolItemDetail.setItemScore(score);
        kfPatrolItemDetail.setItemSort(this.sort);
        kfPatrolItemDetail.setUpdateTime(new Date());
        kfPatrolItemDetail.setKfPatrolItemId(itemId);
        return kfPatrolItemDetail;
    }

    /**
     * 构建KfPatrolItem
     *
     * @param itemName
     * @param parentId
     * @param rootPath
     * @param patrolSceneType
     * @return
     */
    private KfPatrolItemInfo initKfPatrolItem(String itemName, Integer parentId, Integer rootPath, Integer patrolSceneType) {
        KfPatrolItemInfo info = new KfPatrolItemInfo();
        info.setDeleteFlag(0);
        info.setCreateTime(new Date());
        info.setBusBusinessId(this.busBusinessId);
        info.setCreateUserUuid(this.createUser);
        info.setModifyTime(new Date());
        info.setModifyUserUuid(this.createUser);
        info.setPatrolSceneType(patrolSceneType);
        info.setUpdateTime(new Date());
        info.setParentId(parentId);
        info.setRootDepth(rootPath);
        info.setItemName(itemName);
        return info;
    }


}
